<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="校区">
        <el-select v-model="queryParams.campusId"
                   @change="campusHandleChange"
                   placeholder="校区">
          <el-option
            v-for="item in campusList"
            :key="item.id"
            :label="item.campusName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学科">
        <el-select v-model="queryParams.subjectId"
                   @change="subjectHandleChange"
                   placeholder="学科">
          <el-option
            v-for="item in subjectsList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="老师类型">
        <el-select v-model="queryParams.teacherType" placeholder="老师类型">
          <el-option
            v-for="item in teacherTypeList"
            :key="item.id"
            :label="item.typeName"
            :value="item.teacherType"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="老师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日报时间" prop="reportTime">
        <el-date-picker clearable
                        v-model="queryParams.reportTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:report:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:report:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:report:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:report:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="老师名称" align="center" prop="teacherName"/>
      <el-table-column label="日报内容" align="center" prop="content"/>
      <el-table-column label="日报时间" align="center" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:report:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:report:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老师日报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form label-position="center" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日报时间" prop="reportTime">
          <el-date-picker clearable
                          v-model="form.reportTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择日报时间">
          </el-date-picker>
        </el-form-item>
        <!--教学跟踪-->
        <el-form-item label="教学跟踪">
          <el-card class="box-card">
            <div v-for="item in teachingTracking" style="display: flex;align-items: center;padding: 0;">
              <div>
                <div style="display:flex;align-items: center;">
                  <el-select v-model="item.classId"
                             @change="classesHandleChange(item)"
                             placeholder="班级"
                             style="padding:0"
                  >
                    <el-option
                      v-for="item in classes"
                      :key="item.id"
                      :label="item.className"
                      :value="item.id"
                    ></el-option>
                  </el-select>

                  -
                  <el-select v-model="item.studentId"
                             @change="studentHandleChange(item)"
                             placeholder="学生">
                    <el-option
                      v-for="te in item.students"
                      :key="te.id"
                      :label="te.studentName"
                      :value="te.id"
                    ></el-option>
                  </el-select>
                  -
                  <el-select v-model="item.questionType"
                             placeholder="请选择问题类型">
                    <el-option
                      v-for="dict in dict.type.sys_question_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value">
                    </el-option>
                  </el-select>
                  -
                  <el-date-picker clearable
                                  v-model="item.happenTime"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="请选择时间">
                  </el-date-picker>
                  -
                  <el-input v-model="item.deductPoints" style="width: 50px"></el-input>
                </div>

                <div style="display: flex;align-items: center;">
                  <el-input
                    v-model="item.remark"
                    type="textarea"
                    autosize
                    placeholder="请输入内容">
                  </el-input>
                  <el-button size="mini" type="primary" @click="insertCard(item)">+加一行</el-button>
                  <el-button size="mini" type="danger" @click="removeCard(item)">-减一行</el-button>
                </div>
              </div>
            </div>
          </el-card>
        </el-form-item>
        <!--就业跟踪-->
        <el-form-item label="就业跟踪">
          <el-card class="box-card">
            <div v-for="item in employmentTracking" style="display: flex;align-items: center;padding: 0;">
              <div>
                <div style="display:flex;align-items: center;">
                  <el-select v-model="item.classId"
                             @change="classesHandleChange(item)"
                             placeholder="班级"
                             style="padding:0"
                  >
                    <el-option
                      v-for="item in classes"
                      :key="item.id"
                      :label="item.className"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                  -
                  <el-select v-model="item.studentId"
                             @change="studentHandleChange(item)"
                             placeholder="学生">
                    <el-option
                      v-for="te in item.students"
                      :key="te.id"
                      :label="te.studentName"
                      :value="te.id"
                    ></el-option>
                  </el-select>
                  -
                  <el-select v-model="item.trackPurpose" placeholder="请选择追踪目的">
                      <el-option v-for="(option, index) in trackPurposesOptions"
                                 :key="index"
                                 :label="option.label"
                                 :value="option.value">
                      </el-option>
                    </el-select>
                 - <el-date-picker clearable
                                  v-model="item.trackDate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="请选择时间">
                  </el-date-picker>
                  -
                  <el-select v-model="item.studentStatus" placeholder="请选择学生状态">
                    <el-option v-for="(status, index) in studentStatusOptions" :key="index" :label="status.label"
                               :value="status.value">
                    </el-option>
                  </el-select>
                </div>

                <div style="display: flex;align-items: center;">
                  <el-input
                    v-model="item.trackRemark"
                    type="textarea"
                    autosize
                    placeholder="请输入内容">
                  </el-input>
                  <el-button size="mini" type="success" @click="insertEmploymentCard(item)">+加一行</el-button>
                  <el-button size="mini" type="danger" @click="removeEmploymentCard(item)">-减一行</el-button>
                </div>
              </div>
            </div>
          </el-card>
        </el-form-item>
        <el-form-item label="日报内容">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="对今日工作的总结和明日工作的安排"
            v-model="form.content">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReport, getReport, delReport, addReport, updateReport} from "@/api/teacher/report";
import {listCampus, listTeacherType} from "@/api/teacher/teacher";
import {getClassAndStudents} from "@/api/student/student";

export default {
  dicts: ['sys_question_type','sys_student_status', 'sys_track_purpose'],
  name: "Report",
  data() {
    return {
      classes:[],
      studentStatusOptions: [
        { label: '在读未毕业', value: 0 },
        { label: '留级在读', value: 1 },
        { label: '毕业未就业', value: 2 },
        { label: '已就业', value: 3 }
      ],
      trackPurposesOptions: [
        { label: '简历修改', value: 0 },
        { label: '录音分析', value: 1 },
        { label: '就业指导', value: 2 }
      ],
      employmentTracking: [{
        id: 1,
        className:null,
        studentName:null,
        classId: null,
        studentId: null,
        trackPurpose: null,
        trackDate: null,
        studentStatus: null,
        trackRemark: null
      }],
      teachingTracking: [{
        id: 1,
        className:null,
        studentName:null,
        classId: null,
        studentId: null,
        questionType: null,
        happenTime: null,
        deductPoints: null,
        remark: null,
        students:[]
      }],
      teacherTypeList: [],
      campusList: [],
      subjectsList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师日报表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        teacherName: null,
        content: null,
        teacherId: null,
        reportTime: null,
        createTime: null,
        campusId: null,
        subjectId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    insertEmploymentCard(item) {
      this.employmentTracking.push({
        id: item.id++,
        className:null,
        studentName:null,
        classId: null,
        studentId: null,
        trackPurpose: null,
        trackDate: null,
        studentStatus: null,
        trackRemark: null
      })
    },
    removeEmploymentCard(item) {
      for (let i = 0; i < this.employmentTracking.length; i++) {
        if (this.employmentTracking[i].id == item.id) {
          this.employmentTracking.splice(i, 1);
          if (this.employmentTracking.length == 0) {
            this.employmentTracking.push({
              id: 1,
              className:null,
              studentName:null,
              classId: null,
              studentId: null,
              trackPurpose: null,
              trackDate: null,
              studentStatus: null,
              trackRemark: null
            })
          }
          return;
        }
      }
    },

    //减少一行教学追踪
    removeCard(res) {
      for (let i = 0; i < this.teachingTracking.length; i++) {
        if (this.teachingTracking[i].id == res.id) {
          this.teachingTracking.splice(i, 1);
          if (this.teachingTracking.length == 0) {
            this.teachingTracking.push({
              id: 1,
              classId: null,
              studentId: null,
              questionType: null,
              happenTime: null,
              score: null,
              remark: null
            })
          }
          return;
        }
      }
    },
    //添加一行教学追踪
    insertCard(item) {
      this.teachingTracking.push({
        id: item.id++,
        classId: null,
        studentId: null,
        questionType: null,
        happenTime: null,
        score: null,
        remark: null
      })
    },
    loadTeacherTypes() {
      listTeacherType().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.teacherTypeList = data;
        }
      })
    },
    subjectHandleChange(res) {
      this.form.subjectId = res;
    },

    //选中校区获取学科
    campusHandleChange(res) {
      for (let i = 0; i < this.campusList.length; i++) {
        if (this.campusList[i].id == res) {
          this.subjectsList = this.campusList[i].subjects;
          this.form.campusId = res;
          return;
        }
      }
    },
    //选中班级
    classesHandleChange(item) {
      for (let i = 0; i < this.classes.length; i++) {
        if (this.classes[i].id == item.classId) {
          item.students = this.classes[i].studentList;
          item.className = this.classes[i].className;
          return;
        }
      }
    },
    //选中学生
    studentHandleChange(item) {
      for (let i = 0; i < item.students.length; i++) {
        if (item.students[i].id == item.studentId) {
          item.studentName = item.students[i].studentName;
          return;
        }
      }
    },

    //查询校区
    getListCampus() {
      listCampus().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.campusList = data;
        }
      })
    },


    /** 查询老师日报列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherName: null,
        content: null,
        teacherId: null,
        reportTime: null,
        createTime: null,
        teacherType: null,
        campusId: this.form.campusId,
        subjectId: this.form.subjectId
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.employmentTracking=[{
        id: 1,
        className:null,
        studentName:null,
        classId: null,
        studentId: null,
        trackPurpose: null,
        trackDate: null,
        studentStatus: null,
        trackRemark: null
      }];
        this.teachingTracking=[{
        id: 1,
        className:null,
        studentName:null,
        classId: null,
        studentId: null,
        questionType: null,
        happenTime: null,
        deductPoints: null,
        remark: null,
        students:[]
      }];
      this.open = true;
      this.title = "添加老师日报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师日报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.teachingTracking = this.teachingTracking;
      this.form.employmentTracking = this.employmentTracking;
       this.$refs["form"].validate(valid => {
         if (valid) {
           if (this.form.id != null) {
             updateReport(this.form).then(response => {
               this.$modal.msgSuccess("修改成功");
               this.open = false;
               this.getList();
             });
           } else {
             addReport(this.form).then(response => {
               this.$modal.msgSuccess("新增成功");
               this.open = false;
               this.getList();
             });
           }
         }
       });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师日报编号为"' + ids + '"的数据项？').then(function () {
        return delReport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    },
    loadClassAndStudents() {
      getClassAndStudents().then(res => {
        let {code, rows} = res;
        if (code == 200) {
          this.classes = rows;
        }
      })
    }
  },
  mounted() {
    this.getListCampus();
    this.loadTeacherTypes();
    this.loadClassAndStudents();
  }
};
</script>
<style>

</style>
